﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Common;

public partial class tools_sysfunctioneditor : System.Web.UI.Page
{
    public string PageTitle = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Profile.AccountInfo.UserName != "admin")
        //{
        //    Response.Redirect("../unauthorized.htm?errcode=5");
        //    Response.End();
        //}

        Response.Expires = -1;

        if (!IsPostBack)
        {
            DropdownListDataBind();

            if (RequestBase.GetQueryString("otype") == "modify")
            {
                Initialize();
                PageTitle = "修改";
            }

            if (RequestBase.GetQueryString("otype") == "add")
                PageTitle = "新增";
        }
    }

    #region 下拉菜单数据邦定
    void DropdownListDataBind()
    {
        DataTable dt = BLL.CommonBLL.Select(
            "System_FunctionCategory",
            "FunctionCategoryId, CategoryName",
            " ORDER BY LineNum ASC");
        foreach (DataRow dr in dt.Select(""))
        {
            drplCategories.Items.Add(new ListItem(dr["CategoryName"].ToString(), dr["FunctionCategoryId"].ToString()));
        }
        dt.Dispose();
    }
    #endregion

    #region 数据初始化
    /// <summary>
    /// 数据邦定
    /// </summary>
    void Initialize()
    {
        if (RequestBase.GetQueryString("functionid") != "")
        {
            DataTable dt = BLL.CommonBLL.Select("System_FunctionBase", "*", " AND FunctionId = '" + RequestBase.GetQueryString("functionid").Trim() + "'");
            if (dt.Rows.Count > 0)
            {
                txtFunctionName.Text = dt.Rows[0]["FunctionName"].ToString().Trim();
                txtLowerName.Text = dt.Rows[0]["LoweredFunctionName"].ToString().Trim();
                drplCategories.SelectedValue = dt.Rows[0]["FunctionCategoryId"].ToString();
                txtResume.Text = dt.Rows[0]["Resume"].ToString();
                txtOriginalLowerName.Value = dt.Rows[0]["LoweredFunctionName"].ToString().Trim();
                txtLineNum.Text = dt.Rows[0]["LineNum"].ToString();
            }
            dt.Dispose();
        }
    }
    #endregion

    #region 保存数据
    protected void butBegin_ServerClick(object sender, EventArgs e)
    {
        string result = "";
        string SQL = "";
        ///添加模式
        if (RequestBase.GetQueryString("otype") == "add")
        {
            result = ValidateExist(txtLowerName.Text.Trim().ToLower());
            if (result != "")
            {
                JScript.Alert(result);
                return;
            }

            SQL = "INSERT System_FunctionBase Values(newid(), '" + drplCategories.SelectedValue.Trim() + "', '" + txtFunctionName.Text.Trim() + "','" + txtLowerName.Text.Trim().ToLower() + "', '" + txtLineNum.Text.Trim() + "' ,'" + txtResume.Text + "')";
            try
            {
                DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.ConnectionStringMembership, CommandType.Text, SQL, null);
                result = "添加成功!";
            }
            catch (Exception err)
            {
                JScript.Alert(err.Message);
                return;
            }
        }

        ///修改模式
        if (RequestBase.GetQueryString("otype") == "modify" && RequestBase.GetQueryString("functionid") != "")
        {
            if (txtOriginalLowerName.Value != txtLowerName.Text.Trim().ToLower())
            {
                result = ValidateExist(txtLowerName.Text.Trim().ToLower());
                if (result != "")
                {
                    JScript.Alert(result);
                    return;
                }
            }

            SQL = "UPDATE System_FunctionBase SET FunctionCategoryId = '" + drplCategories.SelectedValue.Trim() + "', FunctionName = '" + txtFunctionName.Text.Trim() + "', LoweredFunctionName = '" + txtLowerName.Text.Trim().ToLower() + "', LineNum = '" + txtLineNum.Text.Trim() + "', Resume = '" + txtResume.Text + "' WHERE FunctionId = '" + RequestBase.GetQueryString("functionid").Trim() + "'";
            try
            {
                DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.ConnectionStringMembership, CommandType.Text, SQL, null);
                result = "修改成功!";
            }
            catch (Exception err)
            {
                JScript.Alert(err.Message);
                return;
            }
        }

        JScript.AlertAndReturnValueToParent(result, "window.dialogArguments.document.getElementById(\"RefreshItems\").click(); window.close();");
    }
    /// <summary>
    /// 验证名称是否重复
    /// </summary>
    /// <param name="loweredName"></param>
    /// <returns></returns>
    private string ValidateExist(string loweredName)
    {
        int exist = (int)BLL.CommonBLL.ExecuteScalar("vw_SystemFunctions", "COUNT(1)", " AND LoweredFunctionName = '" + loweredName.ToLower().Trim() + "'", true);
        
        if (exist > 0)
            return "模块英文简称:" + loweredName + "已存在,该项不允许重复!";
        else
            return "";
    }
    #endregion
}
